﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;

namespace com.susi.util.toolkit {
    public class GroupingAdapter<TKey, TElement> : IGrouping<TKey, TElement> {
        [NotNull] private IEnumerable<TElement> Elements;
        [NotNull] private Func<TKey>            _Key;
        [NotNull] public  TKey                  Key => _Key();

        public GroupingAdapter([NotNull] Func<TKey> key, [NotNull] IEnumerable<TElement> element)
        {
            Elements = element ?? throw new ArgumentNullException(nameof(element));
            _Key     = key ?? throw new ArgumentNullException(nameof(key));
        }

        public GroupingAdapter([NotNull] TKey key, [NotNull] IEnumerable<TElement> element)
        {
            Elements = element ?? throw new ArgumentNullException(nameof(element));
            
            _Key = () => key;
        }

        public IEnumerator<TElement> GetEnumerator()
        {
            return Elements.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }
}